import java.util.Scanner;
import java.io.*;
import java.util.StringTokenizer;

public class W9Test {
    public static void main(String[] args){

        String num1,num3,num4,num5;
        int a = 0,b = 0,c = 0;
        try
        {
            Scanner scan = new Scanner(System.in);
            File file = new File("e:\\wang.txt");
            OutputStream outputstream = new FileOutputStream(file);
            BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(outputstream);

            if (!file.exists())
            {
                file.createNewFile();
            }//判断文件是否存在，如果不存在就新建一个

            System.out.print("Enter some number: ");
            num1 = scan.nextLine();
            StringTokenizer strs = new StringTokenizer(num1," ");
            int length = strs.countTokens();
            int[] num2 = new int[length];//输入一串数字，通过Tokenizer方法，以空格为分隔符将每一个整数分开，并创建一个新的整型数组str2

            while (strs.hasMoreTokens())
            {
                for(int y = 0; y < length; y++)
                {
                    String string = strs.nextToken();
                    num2[y] = Integer.parseInt(string);
                }
            }//完成一个循环，判断strs中是否还有字符，如果还有就存入数组str2

            num3 = "";
            for(int str : num2)
            {
                num3 += str + "  ";
            }
            System.out.println("排序前：" + num3);
            bufferedoutputstream.write(num3.getBytes());
            bufferedoutputstream.flush();

            Reader reader = new FileReader(file);
            BufferedReader bufferedreader = new BufferedReader(reader);
            StringTokenizer strss = new StringTokenizer(bufferedreader.readLine());//将str3写入bufferedreader中，通过其自带的readLine方法读取该字符串

            while(strss.hasMoreTokens())
            {
                num2[c] = Integer.parseInt(strss.nextToken());
                c++;
            }//将

            sorting(num2);

            num5 = "";
            for(int str : num2)
            {
                num5 += str + "  ";
            }
            System.out.println("排序后：" + num5);
            num4 = "\n" + num5;
            bufferedoutputstream.write(num4.getBytes());
            bufferedoutputstream.flush();
            bufferedoutputstream.close();
        }
        catch (IOException e)
        {
            System.out.println("错误!!!\n" + e.getMessage());
        }
        catch (NumberFormatException e)
        {
            System.out.println("错误!!!\n" + e.getMessage());
        }
    }

    private static void sorting(int[] str)
    {
        for(int i = 0; i <= str.length - 1; i++)
        {
            int temp = str[i];
            for(int j = i + 1; j < str.length; j++)
                if (str[i] > str[j])
                {
                    str[i] = str[j];
                    str[j] = temp;
                }
        }//fen'bi'we'e

    }

}
